package com.zzuli.admin.sersvice.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzuli.admin.common.ResultSuccess;
import com.zzuli.admin.entity.User;
import com.zzuli.admin.mapper.UserMapper;
import com.zzuli.admin.sersvice.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @ApiOperation("查询用户")
    @Override
    public Map<String,Object> quryUser(int page, int limit,String nickName,String email) {
        Page<User> userPage = PageHelper.startPage(page, limit);
        List<User> users = userMapper.queryUser(nickName,email);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",userPage.getResult());
        map.put("count",userPage.getTotal());
        return map;
    }

    @ApiOperation("添加用户")
    @Override
    public String addUser(User user) {
        user.setId(IdUtil.getSnowflakeNextId());
        userMapper.addUser(user);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        return JSON.toJSONString(map);
    }

    @ApiOperation("删除用户")
    @Override
    public String removeUser(Long id) {
        userMapper.removerUser(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        return JSON.toJSONString(map);
    }

    @ApiOperation("更新用户")
    @Override
    public String updateUser(User user) {
        userMapper.updateUser(user);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        return JSON.toJSONString(map);
    }
}
